package com.itc.bpm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itc.bpm.bean.BpmProcessInstanceExt;
import com.itc.bpm.dto.BpmFactoryCountDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface BpmProcessInstanceExtMapper extends BaseMapper<BpmProcessInstanceExt> {
    @Update("update bpm_process_instance_ext set status=#{status} where processInstanceId=#{processInstanceId}")
    Integer updateStatus(@Param("processInstanceId") String processInstanceId,@Param("status") Integer status);

    /**
     * 校验当前业务是否已经发起流程或者流程已完成
     * @param businessKey
     * @return
     */
    @Select("select count(id) from bpm_process_instance_ext where service_id=#{businessKey} and bpm_model_id=#{modelId} and status=0")
    Integer checkServiceIsExists(@Param("businessKey") String businessKey,@Param("modelId")String modelId);

    @Select("SELECT \n" +
            "\tSUM( CASE WHEN status = 0 THEN 1 ELSE 0 END ) AS progress_num,\n" +
            "\tSUM( CASE WHEN status != 0 THEN 1 ELSE 0 END ) AS complete_num\n" +
            "FROM\n" +
            "\tbpm_process_instance_ext where category like concat(#{code},'%')")
    BpmFactoryCountDto countProcessInstanceInfo(@Param("code") String code);
}
